package com.atguigu.tingshu.order.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.order.OrderDerate;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.order.mapper.OrderInfoMapper;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 三种商品（VIP会员、专辑、声音）订单结算,渲染订单结算页面
     *
     * @param tradeVo (购买项目类型、购买项目ID、声音数量)
     * @return 订单VO信息
     */
    @Override
    public Result<OrderInfoVo> trade(TradeVo tradeVo) {
        //首先，new一个订单视图orderVo
        OrderInfoVo orderVo = new OrderInfoVo();
        //订单最主要的还是金额，所以我们先把原价，优惠价，现价初始化，用bigdecimal
        BigDecimal originalAmount = new BigDecimal("0.0");//原价
        BigDecimal derateAmount = new BigDecimal("0.0");//减免金额
        BigDecimal  orderAmount = new BigDecimal("0.0");//订单金额
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();//订单细节
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();//订单优惠
        //因为有3中订单类型，所以我们先判断订单的类型
        String itemType = tradeVo.getItemType();
        if (itemType.equals(SystemConstant.ORDER_ITEM_TYPE_VIP)){
            //vip订单
            //先获取是那种vip类型
            VipServiceConfig vipServiceConfig = userFeignClient.getVipServiceConfig(tradeVo.getItemId()).getData();
            Assert.notNull(vipServiceConfig,"会员套餐{}不存在", tradeVo.getItemId());
            //再获取vip原价给初始值赋值
            originalAmount=vipServiceConfig.getPrice();
            //再获取最终价格
            orderAmount=vipServiceConfig.getDiscountPrice();
            //判断pareTo==1
            if (originalAmount.compareTo(orderAmount)==1){//原价大于折后价
                derateAmount=originalAmount.subtract(orderAmount);//优惠金额=原价-折后价
                //再封装优惠金额list
                OrderDerateVo orderDerateVo = new OrderDerateVo();
                orderDerateVo.setDerateAmount(derateAmount);
                orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_ALBUM_DISCOUNT);
                orderDerateVo.setRemarks("套餐优惠："+derateAmount);

                orderDerateVoList.add(orderDerateVo);
            }
            //再封装订单细节list
            OrderDetailVo orderDetailVo = new OrderDetailVo();
            orderDetailVo.setItemId(tradeVo.getItemId());
            orderDetailVo.setItemName("会员套餐："+vipServiceConfig.getName());
            orderDetailVo.setItemPrice(orderAmount);
            orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());

            orderDetailVoList.add(orderDetailVo);



        }else if (itemType.equals(SystemConstant.ORDER_ITEM_TYPE_ALBUM)){
            //3.TODO 处理购买商品类型为：专辑

        }else if (itemType.equals(SystemConstant.ORDER_ITEM_TYPE_TRACK)){
            //4.TODO 处理购买商品类型为：声音

        }

        //封装到tradeVo
        orderVo.setOriginalAmount(originalAmount);
        orderVo.setDerateAmount(derateAmount);
        orderVo.setOrderAmount(orderAmount);

        orderVo.setOrderDetailVoList(orderDetailVoList);
        orderVo.setOrderDerateVoList(orderDerateVoList);

        //返回tradeVo
        return Result.ok(orderVo);
    }
}
